﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace CRMSimple.LogService.DataCacheExtensions.FileDataCache
{
    public static class FileDataCacheRootExtensions
    {
        /// <summary>
        /// 获取物理绝对路径
        /// </summary>
        /// <param name="path">相对应用程序路径</param>
        public static string GetPhysicalFilePath(this FileDataCacheRoot fileConfigurationRoot, string path)
        {
            return fileConfigurationRoot.CombinePath(path);
        }
        /// <summary>
        /// 拼接路径
        /// </summary>
        public static string CombinePath(this FileDataCacheRoot fileConfigurationRoot, string path)
        {
            return Path.Combine(fileConfigurationRoot.BasePath, path);
        }
        public static string StreamToString(this FileDataCacheRoot fileConfigurationRoot, Stream stream)
        {
            try
            {
                stream.Seek(0, SeekOrigin.Begin);
                var reader = new StreamReader(stream);

                return reader.ReadToEnd();
            }
            catch (Exception)
            {
                return null;
                throw;
            }

        }
        public static string StreamToStringLine(this FileDataCacheRoot fileConfigurationRoot, Stream stream,out long postion)
        {
            try
            {
                stream.Seek(0, SeekOrigin.Begin);
                
                var reader = new StreamReader(stream);
                var line = reader.ReadLine();
                postion = stream.Position;
                return line;
            }
            catch (Exception)
            {
                postion = 0;
                return null;
                throw;
            }

        }
        /// <summary>
        /// 校验路径文件夹是否存在，不存在创建
        /// </summary>
        /// <param name="path">相对应用程序路径</param>
        public static string CheckDirectory(this FileDataCacheRoot fileConfigurationRoot, string path)
        {
            string directory = System.IO.Path.GetDirectoryName(path);
            if (!Directory.Exists(directory))
                Directory.CreateDirectory(directory);
            return path;
        }
       

        public static FileDataCacheRoot UseWriteCache(this FileDataCacheRoot fileConfigurationRoot, int count)
        {
            fileConfigurationRoot.CacheCount = count;
            return fileConfigurationRoot;
        }
        ///// <summary>
        ///// 多行写入
        ///// </summary>
        //public static string GetProducterDataWithCache(this FileDataCacheRoot fileConfigurationRoot)
        //{
        //    //if (fileConfigurationRoot.CacheCount > 1)
        //    //{
        //    //    StringBuilder sb = new StringBuilder(fileConfigurationRoot.CacheCount);
        //    //    for (int i = 0; i < fileConfigurationRoot.CacheCount; i++)
        //    //    {
        //    //        sb.Append(fileConfigurationRoot.GetProducterData() + fileConfigurationRoot.Newline);
        //    //    }
        //    //    return sb.ToString();
        //    //}
        //    //else
        //    //    return fileConfigurationRoot.GetProducterData();
        //}
        public static void SetReadOnly(this FileDataCacheRoot fileConfigurationRoot, string path)
        {
            try
            {
                File.SetAttributes(path, FileAttributes.ReadOnly);
            }
            catch (Exception ex)
            {
                
                throw;
            }
            
        }
    }
}
